<?php

class MprankAdmin extends AdminBase
{
	private $nohf = [];
	private $mysql = null;
	private $api = null;

	public function __construct()
	{
		parent::__construct();
		if(!in_array(N_ACTION,$this->nohf)){
			n_mod('skin')->header('admin');
		}
		$this->api = n_api("mprank");
		$this->mysql = n_api('mysql')->cname('weixin');
		$this->weixin = n_api('weixin');
	}

	public function sta(){
		$sta = $this->mysql->selectIndex("select cid,count(*) as total from weixin_main group by cid",'cid');
		$cate = $this->weixin->getCategories();

		$rs = array();
		foreach($cate as $key=>$val){
			$rs[$val['fid']]['name'] = $val['first_category'];
			$rs[$val['fid']]['sub'][] = array("id"=>$val['id'],"name"=>$val['second_category'],"num"=>$sta[$val['id']]['total']);
		}

		$this->_html("sta",$rs);
	}
	
	public function mplist(){

		$stime = microtime(true);
		$param = $this->param('
			cid:uint:ignore,
			date:char:ignore,
			page:page
		');
		$data['page'] = $param['page'];
		$data['cid'] = empty($param['cid'])?1:$param['cid'];
		$data['perpage'] = $perpage = 50;
		$categories = $this->weixin->getCategories();

		$category = $categories[$data['cid']];

		if (isset($param['date'])) {
			$date = strtotime($param['date']);
		} else {
			$date = $this->api->getMaxRankDay();
		}

		if (isset($param['page'])) {
			$page = $param['page'];
		} else {
			$page = 1;
		}

		$start = ($page -1) * $perpage;
		$data['categories'] = $categories;
		$data['category'] = $category;
		$data['date'] = $date;

		//echo $data['total'] = $this->weixin->get_total_count();

		$data['rank_mplist'] = $this->api->getMprankByCategoryId($category['id'], $date, 'day', $start, $perpage);
		$data['page'] = $page;
		$total = $this->mysql->fetchFirst("select count(*) as total from weixin_main where cid={$category['id']}");
		$data['paging'] = n_api("common")->paging(n_url('mprank','mplist',array('cid'=>$category['id'])),$page,$total['total'],$perpage);

		$this->_html("mplist",$data);
	}

	public function __destruct(){
		//加入标准尾。
		if(!in_array(N_ACTION,$this->nohf) && 'page'==N_TYPE){
			n_mod('skin')->footer();
		}
	}
}